package com.becca.cake.core.excel.excelReader;

import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;

import com.becca.cake.util.DateUtils;

/**
 * Excel工具类
 * @author hejinguo
 * @version $Id: ExcelUtils.java, v 0.1 2014-4-21 下午11:13:41
 */
public class ExcelUtils {
    /**
     * 获取读取Excel起始行
     * @param statrRow
     */
    public static int getStartRow(int startRow) {
        if (startRow < 0) {
            return 0;
        }
        return startRow;
    }

    /**
     * 获取读取起始列
     * @param startColumn
     * @return
     */
    public static int getStartColumn(int startColumn) {
        if (startColumn < 0) {
            return 0;
        }
        return startColumn;
    }

    /**
     * 类型转换
     * @param val
     * @param c
     * @return
     */
    public static Object castValue(Object val, Class<? extends Object> c) {
        if (val == null || val.toString().equals("null")) {
            return null;
        }
        if ((java.lang.String.class).equals(c)) {
            return val.toString();
        }
        if ((java.lang.Integer.class).equals(c) || Integer.TYPE.equals(c)) {
            return Integer.valueOf(val.toString());
        }
        if ((java.lang.Double.class).equals(c) || Double.TYPE.equals(c)) {
            String currency = val.toString().replaceAll("\\$|\\￥|\\,", "");
            return Double.valueOf(currency);
        }
        if ((java.lang.Short.class).equals(c) || Short.TYPE.equals(c)) {
            return Short.valueOf(val.toString());
        }
        if ((java.lang.Long.class).equals(c) || Long.TYPE.equals(c)) {
            return Long.valueOf(val.toString());
        }
        if ((java.lang.Float.class).equals(c) || Float.TYPE.equals(c)) {
            return Float.valueOf(val.toString());
        }
        if ((java.lang.Byte.class).equals(c) || Byte.TYPE.equals(c)) {
            return Byte.valueOf(val.toString());
        }
        if ((java.lang.Boolean.class).equals(c) || Boolean.TYPE.equals(c)) {
            return Boolean.valueOf(val.toString());
        }
        if ((java.util.Date.class).equals(c)) {
            return DateUtils.parseDate(String.valueOf(val));
        }
        return null;
    }

    /**
     * Excel cell数据类型转换
     * @param cell
     * @return
     */
    public static Object transCellValue(Cell cell) {
        Object object = null;
        if (cell.getType() == CellType.DATE) {//时间类型格式数据
            DateCell dc = (DateCell) cell;
            object = DateUtils.excelDateFormat(dc.getDate());
        } else {
            if (null != cell.getContents()) {
                object = cell.getContents().trim();
            }
        }
        return object;
    }

}
